package com.xing.controller;

import com.xing.DTO.RoleSaveDTO;
import com.xing.DTO.RoleUpdateDTO;
import com.xing.Utils.Result;
import com.xing.entity.SysRoleEntity;
import com.xing.service.SysRoleService;
import com.xing.utils.PageData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "角色服务")
@RestController
@RequestMapping(value = "role")
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;

    @Operation(summary = "获取全部角色信息")
    @GetMapping(value = "listAll")
    @PreAuthorize("hasAuthority('admin')")
    public Result listAll() {
        List<SysRoleEntity> roleList = roleService.listAll();
        return new Result().ok(roleList);
    }


    @Operation(summary = "分页获取角色信息")
    @GetMapping(value = "listByPage")
    @PreAuthorize("hasAuthority('admin')")
    public Result listByPage(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        PageData<SysRoleEntity> roleList = roleService.listByPage(pageNum, pageSize);
        return new Result().ok(roleList);
    }


    @Operation(summary = "新增角色信息")
    @PostMapping(value = "save")
    @PreAuthorize("hasAuthority('admin')")
    public Result saveRole(@RequestBody RoleSaveDTO roleSaveDTO) {
        return roleService.saveRole(roleSaveDTO);
    }


    @Operation(summary = "修改角色信息")
    @PutMapping(value = "updateById")
    @PreAuthorize("hasAuthority('admin')")
    public Result updateById(@RequestBody RoleUpdateDTO roleUpdateDTO) {
        return roleService.updateById(roleUpdateDTO);
    }


    @Operation(summary = "删除角色信息")
    @DeleteMapping(value = "deleteById")
    @PreAuthorize("hasAuthority('admin')")
    public Result deleteById(@RequestParam Long id) {
        return roleService.deleteById(id);
    }


}
